package com.flower.enjoyment.flower.service.impl;

import com.alibaba.fastjson.JSON;
import com.flower.enjoyment.flower.entity.AssessImg;
import com.flower.enjoyment.flower.entity.ShopInfo;
import com.flower.enjoyment.flower.entity.UserAssess;
import com.flower.enjoyment.flower.entity.UserHobby;
import com.flower.enjoyment.flower.entityDo.AssessImgDo;
import com.flower.enjoyment.flower.entityDo.ShopInfoDo;
import com.flower.enjoyment.flower.entityDo.UserAssessDo;
import com.flower.enjoyment.flower.mapper.AssessImgMapper;
import com.flower.enjoyment.flower.mapper.ShopMapper;
import com.flower.enjoyment.flower.mapper.UserAssessMapper;
import com.flower.enjoyment.flower.mapper.UserHobbyMapper;
import com.flower.enjoyment.flower.service.UserAssessService;
import com.flower.enjoyment.flower.utils.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserAssessServiceImpl implements UserAssessService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserAssessMapper userAssessMapper;
    @Autowired
    private AssessImgMapper assessImgMapper;
    @Autowired
    private UserHobbyMapper userHobbyMapper;
    @Autowired
    private ShopMapper shopMapper;
    /**
     * 获取首页评论信息
     *
     * @return
     */
    @Override
    public List<UserAssessDo> selectUserAssessListByHome(String userId) {
        return userAssessMapper.selectUserAssessListByHome(userId);
    }

    /**
     * 评价详情
     * @param id
     * @return
     */
    public UserAssessDo selectByPrimaryKey(String id){

        UserAssessDo assessDo = userAssessMapper.selectByPrimaryKey(id);
        if (assessDo!=null){
        assessDo.setDoList(userAssessMapper.selectAssessDetailById(id));
        }
        return assessDo;
    }

    /**
     * 新增评价
     * @param record
     * @return
     */
    @Transactional
    public int insert(UserAssess record, List<AssessImgDo> assessImgDos) throws Exception{

        AssessImg assessImg=null;
        if (assessImgDos !=null) {
            for (int i = 0; i < assessImgDos.size(); i++) {
                assessImg = new AssessImg();
                assessImg.setCreateTime(new Date());
                assessImg.setId(RandomUtil.getUuid());
                assessImg.setAssessId(record.getId());
                assessImg.setImg(assessImgDos.get(i).getImg());
                assessImg.setSort(assessImgDos.get(i).getSort());
                assessImgMapper.insert(assessImg);
            }
        }
        if(StringUtils.isNotBlank(record.getParentId()) && StringUtils.isNotBlank(record.getHuifuUserId())) {
            logger.info("第一次回复评论，该商铺星加1，商铺ID：{1}",record.getShopId());
            ShopInfoDo shopInfoDo = shopMapper.selectByPrimaryKey(record.getShopId());
            logger.info("第一次回复评论，根据商铺ID获取商铺信息：{1}", JSON.toJSONString(record));
            ShopInfo shopInfo = new ShopInfo();
            shopInfo.setId(record.getShopId());
            shopInfo.setStar(shopInfoDo.getStar() + 1);
            shopMapper.updateByPrimaryKeySelective(shopInfo);
        }
        int result = userAssessMapper.insert(record);
        return result;
    }

    /**
     * 新增用户喜欢
     *
     * @param userHobby
     * @return
     */
    @Override

    @Transactional
    public int insertUserHobby(UserHobby userHobby) {
        return userHobbyMapper.insert(userHobby);
    }

    /**
     * 删除用户喜欢
     *
     * @param
     * @return
     */
    @Override

    @Transactional
    public int deleteUserHobby(String assessId,String userId) {
        return userHobbyMapper.deleteUserHobbyById(assessId,userId);
    }

    /**
     * 根据评论ID删除评论
     *
     * @param assessId
     * @return
     */
    @Override
    @Transactional
    public int deleteUserAssess(String assessId,String userId) {
        UserAssessDo userAssessDo = userAssessMapper.selectDetailByUserAndAssessId(userId,assessId);
        if (userAssessDo!=null) {
            int result = assessImgMapper.deleteByAssessId(assessId);
            result += userAssessMapper.deleteByPrimaryKey(assessId);

            return result;
        }else {
            return 0;
        }
    }

    /**
     * 我的评论
     *
     * @param pageNo
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public Page<UserAssessDo> selectUserAssessListUserIdByPage(Integer pageNo, Integer pageSize, String userId) {
        PageHelper.startPage(pageNo, pageSize);
        return userAssessMapper.selectUserAssessListUserIdByPage(userId);
    }

    /**
     * 大家说
     *
     * @param userId
     * @param shopId
     * @return
     */
    @Override
    public Page<UserAssessDo> selectEveryBodySayListByPage(Integer pageNo, Integer pageSize, String userId, String shopId) {
        PageHelper.startPage(pageNo, pageSize);
        return userAssessMapper.selectEveryBodySayListByPage(userId, shopId);
    }
}
